// pages/address/address.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        addressId:0,
        check:true,
        addressList:[]

    },
    radiocon:function(e){
        // var check = this.data.check;
        // check =! check;
        // this.setData({
        //     check:check
        // })
        let index = e.currentTarget.dataset.index
        let address = this.data.addressList[index]
        let isDefault = address.isDefault == 1 ? 0 :1
        wx.put('http://localhost:3000/wx/address/default',{
            addressId:address._id,
            memberId:wx.userInfo._id,
            isDefault
        }).then(res => {
            if(!res.errorMsg){
                this.getAddressList()
            }
        })
    },
    delete(e){///删除默认地址
        wx.showModal({
            title:'删除提示',
            content:'确定删除此地址？',
            success:res =>{
                if(res.confirm){
                    wx.delete('http://localhost:3000/wx/address',{
                        addressId:address._id,
                    }).then(res => {
                        if(!res.errorMsg){
                            this.getAddressList()
                        }
                    })
                }
            }
        })
    },
    getAddressList(){
        wx.get('http://localhost:3000/wx/address').then(res =>{
            this.setData({
                addressList:res
            })
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getAddressList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },
    editaddress(e){
        let index = e.currentTarget.dataset.index
        let address = this.data.addressList[index]
        wx.navigateTo({
          url: '/pages/addaddress/addaddress?addressId=' + address._id,
        })
    },
    addaddress:function(e){
        wx.navigateTo({
          url: "/pages/addaddress/addaddress"
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    getAddressDetail(){
        wx.get('http://localhost:3000/wx/address/detail',{
            addressId:this.data.addressId
        }).then(res => {
            this.setData({
                receiver:res.receiver,
                phone:res.phone,
                city:res.city,
                area:res.area
            })
        })
    },
    onUnload: function (options) {
        if(options.addressId){
            this.setData({
                addressId:options.addressId
            })
            ///获取地址详情
            this.getAddressDetail()
        }

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})